<template>
  <div>
    <el-tree
      ref="tree"
      :data="list"
      show-checkbox
      node-key="id"
      check-strictly
      :default-checked-keys="checkedList"
      :props="defaultProps"
    />

    <div style="text-align: center; margin-top: 30px">
      <el-button type="primary" @click="submitHandler">确定</el-button>
      <el-button @click="closeHandler">取消</el-button>
    </div>
  </div>
</template>

<script>

import { getPermissionDetail } from '@/api/permisson'
import { transArray } from '@/utils'
import { getRoleDetail, assignPerm } from '@/api/setting'

export default {
  name: 'VueAdminTemplateMasterSetdia',

  data() {
    return {
      id: '',
      list: [],
      checkedList: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },

  mounted() {

  },
  created() {
    this.updatePermissionDetail()
  },
  methods: {
    // 获取树形图数据
    async updatePermissionDetail() {
      const res = await getPermissionDetail(this.id)
      // console.log(res)
      if (res.code !== 10000) return
      this.list = transArray(res.data)
      this.updateRoleDetail()
    },
    // 获取当前点击的数据
    async updateRoleDetail() {
      const res = await getRoleDetail(this.id)
      // console.log(res)
      if (res.code !== 10000) return
      this.checkedList = res.data.permIds
    },
    // 提交
    async submitHandler() {
      const list = this.$refs.tree.getCheckedNodes()
      // console.log(list)
      this.checkedList = list.map(item =>  item.id)
      // 发请求
      const res = await assignPerm({ id: this.id, permIds: this.checkedList })
      // console.log(res)
      if (res.code !== 10000) return
      this.$parent.$parent.roleShowDialog = false
    },
    closeHandler() {
      this.$parent.$parent.roleShowDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
